package com.globant.dashboard.persist;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import com.globant.dashboard.model.User;

public class UserDAOImp implements UserDAO {
	
	@Autowired
	private HibernateSessionFactory hibernateSessionFactory;
	
	private Session session = null;
	private Transaction transaction = null;

	public UserDAOImp() {
	}

	@SuppressWarnings("unchecked")
	public List<User> getByCriteria(final String searchParameter, int pageNumber, int pageSize) {
		session =hibernateSessionFactory.openSession();
		Query query = session.createQuery("FROM User WHERE name LIKE :name OR company LIKE :company OR experience LIKE :experience");
		query.setParameter("name", "%" + searchParameter + "%");
		query.setParameter("company", "%" + searchParameter + "%");
		query.setParameter("experience", "%" + searchParameter + "%");
		if(pageSize!=0){
			query = query.setFirstResult(pageSize * (pageNumber - 1));
			query.setMaxResults(pageSize);
		}
		List<User> list = query.list();
		session.close();
		return list;
	}

	public User insert(final User user) {
		session = hibernateSessionFactory.openSession();
		transaction = session.beginTransaction();
		session.save(user);
		transaction.commit();
		session.close();
		return user;
	}

	public User update(final User user) {
		session = hibernateSessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(user);
		transaction.commit();
		session.close();
		return user;
	}

	public User delete(final User user) {
		session =hibernateSessionFactory.openSession();
		user.setEmail("");
		user.setName("");
		transaction = session.beginTransaction();
		session.delete(user);
		transaction.commit();
		session.close();
		return user;
	}

	@SuppressWarnings("unchecked")
	public List<User> getAll(int pageNumber, int pageSize) {
		session =hibernateSessionFactory.openSession();
		Query query = session.createQuery("FROM User");
		if(pageSize!=0){
			query = query.setFirstResult(pageSize * (pageNumber - 1));
			query.setMaxResults(pageSize);
		}
		List<User> list = query.list();
		session.close();
		return list;
	}

	public User getById(int id) {
		session = hibernateSessionFactory.openSession();
		Query query = session.createQuery("FROM User WHERE id=:id");
		query.setInteger("id", id);
		User user = (User) query.uniqueResult();
		session.close();
		return user;
	}

	public User getByEmail(String email) {
		session = hibernateSessionFactory.openSession();
		Query query = session.createQuery("FROM User WHERE email=:email");
		query.setString("email", email);
		User user = (User) query.uniqueResult();
		session.close();
		return user;
	}
	
	public int countUsers(){
		List<User> list = getAll(1, 0);
		return list.size();
	}
	
	public int countUserByCriteria(String criteria){
		List<User> list = getByCriteria(criteria, 0, 0);
		return list.size();
	}
	
}